package com.example.weather;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.StringUtils;
import com.magicsoft.app.wcf.HomeService;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.constant.Constant;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class WeatherDetailActivity extends Activity implements OnClickListener, AMapLocationListener {

	private GridView mHotcityView;
	String ROOT = "http://xinbo.qiniudn.com/";
	String CITIES_URL = ROOT + "cities.txt";
	private ArrayList<FutureWeather> FutureWeatherList = new ArrayList<FutureWeather>();
	private MyFutureAdapter myFutureAdapter;
	private TextView weather_weather;
	private TextView weather_wind;
	private TextView weather_temperature;
	private TextView weather_currentcity;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	private TextView Tv_weather_pm;
	private LocationManagerProxy mLocationManagerProxy;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_weather_detail);

		myFutureAdapter = new MyFutureAdapter();
		initUI();
		String currentCity = SharePreferenceHelper
				.getCurrentCity(WeatherDetailActivity.this);
		getFutureWeather(currentCity);
		ListView mListView = (ListView) findViewById(R.id.listView1);
		mListView.setAdapter(myFutureAdapter);

	}

	private void initUI() {

		weather_currentcity = (TextView) findViewById(R.id.weather_currentcity);
		
		weather_weather = (TextView) findViewById(R.id.weather_weather);
		weather_wind = (TextView) findViewById(R.id.weather_wind);
		weather_temperature = (TextView) findViewById(R.id.weather_temperature);
		Tv_weather_pm = (TextView) findViewById(R.id.Tv_weather_pm);
		
		
		
		String currentCity = SharePreferenceHelper
				.getCurrentCity(WeatherDetailActivity.this);
		weather_currentcity.setText(currentCity);

		findViewById(R.id.weather_back).setOnClickListener(this);
		weather_currentcity.setOnClickListener(this);
		findViewById(R.id.Tv_changecity).setOnClickListener(this);
		findViewById(R.id.ima_locatcity_weather).setOnClickListener(this);
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.placeholder2)
		.showImageForEmptyUri(R.drawable.placeholder2)
		.showImageOnFail(R.drawable.placeholder2).cacheInMemory(true)
		.cacheOnDisc(true).considerExifParams(true)
		// .displayer(new RoundedBitmapDisplayer(20))
		.build();

	}

	private void getFutureWeather(String currentCity) {

		HomeService homeService = new HomeService(WeatherDetailActivity.this);
		homeService.getWeatherList(currentCity,
				new GetOneRecordListener<List<FutureWeather>>() {

					@Override
					public void onFinish(List<FutureWeather> data) {
						FutureWeatherList.clear();
						if (data != null) {
							FutureWeatherList.addAll(data);
							myFutureAdapter.notifyDataSetChanged();
							if (FutureWeatherList.size() != 0) {
								weather_weather.setText(FutureWeatherList.get(0).getWeather());
								weather_wind.setText(FutureWeatherList.get(0).getWind());
								weather_temperature.setText(FutureWeatherList.get(0)
										.getTemperature());
								String weatherPm = SharePreferenceHelper.getWeatherPm(WeatherDetailActivity.this);
								Tv_weather_pm.setText("PM2.5: "+weatherPm);
							}
						}
					}

					@Override
					public void onFailed(String Message) {
						Log.e("ChangeWeatherActivity", "Message =" + Message);
					}
				});
	}

	class MyFutureAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return FutureWeatherList.size() - 1;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View controview, ViewGroup viewGroup) {
			View view = null;
			ViewHolder viewHolder = null;
			FutureWeather futureWeather = FutureWeatherList.get(position + 1);

			if (controview == null) {
				viewHolder = new ViewHolder();
				view = getLayoutInflater().inflate(
						R.layout.future_weather_listltem, null);
				viewHolder.Tv_whichday = (TextView) view
						.findViewById(R.id.Tv_whichday);
				viewHolder.ima_weather = (ImageView) view
						.findViewById(R.id.ima_weather);
				viewHolder.Tv_temperature = (TextView) view
						.findViewById(R.id.Tv_temperature);
				view.setTag(viewHolder);
			} else {
				view = controview;
				viewHolder = (ViewHolder) view.getTag();
			}
			
			viewHolder.Tv_whichday.setText(futureWeather.getDate());
			viewHolder.Tv_temperature.setText(futureWeather.getTemperature());
			if (StringUtils.isImageUrl(futureWeather.getDayPictureUrl())) {
				imageLoader.displayImage(futureWeather.getDayPictureUrl(),
						viewHolder.ima_weather, options);
			}
			return view;
		}

	}

	class ViewHolder {
		TextView Tv_whichday;
		ImageView ima_weather;
		TextView Tv_temperature;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.weather_back:
			Intent data = new Intent();
			if (FutureWeatherList.size() != 0) {
				data.putExtra("weather", FutureWeatherList.get(0)
						.getWeather());
				data.putExtra("wind", FutureWeatherList.get(0)
						.getWind());
				data.putExtra("temperature", FutureWeatherList.get(0)
						.getTemperature());
			}
			setResult(Constant.ChangeHomeCityName, data );
			finish();
			break;
		case R.id.weather_currentcity:
			Intent intent = new Intent();
			intent.setClass(WeatherDetailActivity.this,
					ChangeCityWeatherActivity.class);
			startActivityForResult(intent, Constant.JumpChangeCity_Code);
			break;
		case R.id.Tv_changecity:
			Intent intent1 = new Intent();
			intent1.setClass(WeatherDetailActivity.this,
					ChangeCityWeatherActivity.class);
			startActivityForResult(intent1, Constant.JumpChangeCity_Code);
			break;

		case R.id.ima_locatcity_weather:
			Locationcity();
			break;
		default:
			break;
		}
	}
	
	private void Locationcity() {
		mLocationManagerProxy = LocationManagerProxy.getInstance(this);
		mLocationManagerProxy.setGpsEnable(false);
		mLocationManagerProxy.requestLocationData(
				LocationProviderProxy.AMapNetwork, -1, 15, this);
	}

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {
			if (requestCode == Constant.JumpChangeCity_Code) {
				if (resultCode == Constant.JumpChangeCity_Code) {
					String city = data.getStringExtra(Constant.ChangeCityName);
					getFutureWeather(city);
					weather_currentcity.setText(city);
					if (FutureWeatherList.size() != 0) {
						weather_weather.setText(FutureWeatherList.get(0)
								.getWeather());
						weather_wind
								.setText(FutureWeatherList.get(0).getWind());
						weather_temperature.setText(FutureWeatherList.get(0)
								.getTemperature());
						String weatherPm = SharePreferenceHelper.getWeatherPm(this);
						Tv_weather_pm.setText("PM2.5: "+weatherPm);
					}
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLocationChanged(AMapLocation amapLocation) {
		if (amapLocation != null
				& amapLocation.getAMapException().getErrorCode() == 0) {
			// 回调成功
			String city = amapLocation.getCity();
			weather_currentcity.setText(city);
			getFutureWeather(city);
			SharePreferenceHelper.setCurrentCity(city,
					WeatherDetailActivity.this);
		}
	}

}
